package config

import (
	"new_for_deepseek_rabbitmq/logger"

	"github.com/spf13/viper"
)

type Config struct {
	Server struct {
		Port    string
		Setting string
	}
	Database struct {
		Host         string
		Port         int
		Name         string
		User         string
		Password     string
		MaxIdleConns int
		MaxOpenConns int
		MaxLifetime  int
	}
	Rabbitmq struct {
		Host          string
		Port          int
		User          string
		Password      string
		Vhost         string
		PrefetchCount int
	}
	Tencent struct {
		Lke struct {
			Prefix      string
			BotAppKey   string
			ChatSeeUrl  string
			TemplateExt string
			Region      string
			BotBizId    string
		}
		SecretId  string
		SecretKey string
	}
	Mk struct {
		Url       string
		Username  string
		Password  string
		RsaPublic string
		LoginTime int
		Supplier  string
	}
	Redis struct {
		Host     string
		Port     int
		Password string
		DB       int
	}
}

var (
	DefaultConfig *Config = LoadConfig()
)

func LoadConfig() *Config {
	viper.SetConfigName("config")
	viper.SetConfigType("json")

	// 按优先级依次查找配置文件
	viper.AddConfigPath(".")        // 当前目录
	viper.AddConfigPath("./config") // 相对于执行文件的 config 目录

	if err := viper.ReadInConfig(); err != nil {
		logger.Error("Failed to read config file: %s", err.Error())
		return nil
	}

	config := &Config{}
	if err := viper.Unmarshal(config); err != nil {
		logger.Error("Failed to unmarshal config: %s", err.Error())
		return nil
	}

	return config
}
